local fuping = fk.CreateSkill {

  name = "joy__fuping",

  tags = {  },

}



fuping:addEffect("viewas", {
  name = "joy__fuping",
  anim_type = "special",
  pattern = ".",
  prompt = "#fuping-viewas",
  interaction = function(self,player)
    if type(player:getMark("@$fuping")) ~= "table" then return end
    local all_names, names = player:getMark("@$fuping"), {}
    local used_names = type(player:getMark("fuping-turn")) == "table" and player:getMark("fuping-turn") or {}
    for _, name in ipairs(all_names) do
      if not table.contains(used_names, name) then
        local to_use = Fk:cloneCard(name)
        to_use.skillName = "fuping"
        if ((Fk.currentResponsePattern == nil and player:canUse(to_use) and not player:prohibitUse(to_use)) or
          (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
          table.insertIfNeed(names, name)
        end
      end
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names, all_choices = all_names}
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected < 1 and Fk:getCardById(to_select).type ~= Card.TypeBasic
  end,
  view_as = function(self, player,cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = fuping.name
    return card
  end,
  enabled_at_play = function(self, player)
    local names = player:getMark("@$fuping")
    if type(names) ~= "table" then return false end
    local used_names = type(player:getMark("fuping-turn")) == "table" and player:getMark("fuping-turn") or {}
    for _, name in ipairs(names) do
      if not table.contains(used_names, name) then
        local to_use = Fk:cloneCard(name)
        to_use.skillName = fuping.name
        if player:canUse(to_use) and not player:prohibitUse(to_use) then
          return true
        end
      end
    end
  end,
  enabled_at_response = function(self, player, response)
    local names = player:getMark("@$fuping")
    if type(names) ~= "table" then return false end
    local used_names = type(player:getMark("fuping-turn")) == "table" and player:getMark("fuping-turn") or {}
    for _, name in ipairs(names) do
      if not table.contains(used_names, name) then
        local to_use = Fk:cloneCard(name)
        to_use.skillName = fuping.name
        if (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use)) then
          return true
        end
      end
    end
  end,
  before_use = function(self, player, useData)
    local names = type(player:getMark("fuping-turn")) == "table" and player:getMark("fuping-turn") or {}
    table.insert(names, useData.card.trueName)
    player.room:setPlayerMark(player, "fuping-turn", names)
  end,
})

fuping:addEffect(fk.CardUseFinished, {
  name = "#joy__fuping_trigger",
  events = {fk.CardUseFinished},
  --fuping,
  can_trigger = function(self, event, target, player, data)
    if target == player or not player:hasSkill(fuping.name) or #player:getAvailableEquipSlots() == 0 then return false end
    if data.card.type ~= Card.TypeEquip and table.contains(data:getAllTargets(), player.id) then
      local mark = player:getMark("@$fuping")
      return type(mark) ~= "table" or not table.contains(mark, data.card.trueName)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local all_choices = {"WeaponSlot", "ArmorSlot", "DefensiveRideSlot", "OffensiveRideSlot", "TreasureSlot"}
    local subtypes = {Card.SubtypeWeapon, Card.SubtypeArmor, Card.SubtypeDefensiveRide, Card.SubtypeOffensiveRide, Card.SubtypeTreasure}
    local choices = {}
    for i = 1, 5, 1 do
      if #player:getAvailableEquipSlots(subtypes[i]) > 0 then
        table.insert(choices, all_choices[i])
      end
    end
    table.insert(all_choices, "Cancel")
    table.insert(choices, "Cancel")
    local choice = player.room:askForChoice(player, choices, fuping.name, "#fuping-choice:::" .. data.card.trueName, false, all_choices)
    if choice ~= "Cancel" then
      event:setCostData(self, choice)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(fuping.name)
    room:abortPlayerArea(player, {event:getCostData(self)})
    local mark = type(player:getMark("@$fuping")) == "table" and player:getMark("@$fuping") or {}
    table.insertIfNeed(mark, data.card.trueName)
    room:setPlayerMark(player, "@$fuping", mark)
  end,
})

fuping:addEffect("targetmod", {
  name = "#joy__fuping_targetmod",
  bypass_distances = function(self, player, skill, card, to)
    return player:hasSkill(fuping.name) and #player:getAvailableEquipSlots() == 1
  end,
})

return fuping